package com.example.qxfw.common.service;
import com.example.qxfw.common.util.GetPath;
import com.example.qxfw.dysjrh.config.DmgisConfig;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.Map;
import java.util.Objects;

/**
 * @author 14559
 * @version 1.0
 * @description: TODO
 * @date 2025/1/14 13:13
 */
@RestController
@Service
public class FileRequestService {
    @Autowired
    private DmgisConfig dmgisConfig;

    private String GettqFilepath() {
        String tqFilepath = "";
        String os = System.getProperty("os.name").toLowerCase();
        if (os.contains("win")) {
            tqFilepath = dmgisConfig.getWinpath();
        } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
            tqFilepath = dmgisConfig.getLinuxpath();
        }
        return tqFilepath;
    }

    public byte[] selectData(Map<String, Object> queryMap) {
        String dbName = dmgisConfig.getDbname();
        String collectionName = (String)queryMap.get("collectionName");
        queryMap.remove("collectionName");
//        if (!StringUtil.IsNullOrEmpty((String)queryMap.get("advCode"))) {
        String advCode = (String)queryMap.get("advCode");
        if (Objects.nonNull(advCode) && !advCode.isEmpty()) {
            dbName = dbName + "_" + queryMap.get("advCode");
            queryMap.remove("advCode");
        }

        byte[] result = null;
        String basePath = GettqFilepath();
        String filePath = basePath + File.separator +  "DmgisPubData" + File.separator + dbName + File.separator + collectionName + File.separator;
        filePath = GetPath.action(queryMap, filePath, collectionName);
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            try {
                InputStream in = new FileInputStream(filePath);
                byte[] filebyte = IOUtils.toByteArray(in);
                in.close();
                result = filebyte;
            } catch (IOException var10) {
                var10.printStackTrace();
            }
        } else {
            System.out.println("文件不存在");
        }

        return result;
    }

    public int insertData(Map<String, Object> insertMap) {
        String dbName = dmgisConfig.getDbname();
        String collectionName = (String)insertMap.get("collectionName");
        insertMap.remove("collectionName");
//        if (!StringUtil.IsNullOrEmpty((String)insertMap.get("advCode"))) {
        String advCode = (String)insertMap.get("advCode");
        if (Objects.nonNull(advCode) && !advCode.isEmpty()) {
            dbName = dbName + "_" + insertMap.get("advCode");
            insertMap.remove("advCode");
        }

        String basePath = GettqFilepath();
        String filePath = basePath + File.separator +  "DmgisPubData" + File.separator + dbName + File.separator + collectionName + File.separator;
        filePath = GetPath.action(insertMap, filePath, collectionName);
        byte[] fileByte = (byte[])((byte[])insertMap.get("data"));

        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists() && !file.getParentFile().isDirectory()) {
                file.getParentFile().mkdirs();
            }

            OutputStream os = new FileOutputStream(filePath);
            os.write(fileByte, 0, fileByte.length);
            os.flush();
            os.close();
            return 1;
        } catch (IOException var9) {
            var9.printStackTrace();
            return 0;
        }
    }

    public long deleteData(Map<String, Object> deleteMap) {
        int count = 0;
        String dbName = dmgisConfig.getDbname();
        String collectionName = (String)deleteMap.get("collectionName");
        deleteMap.remove("collectionName");
//        if (StringUtil.IsNullOrEmpty((String)deleteMap.get("advCode"))) {
        String advCode = (String)deleteMap.get("advCode");
        if (Objects.nonNull(advCode) && !advCode.isEmpty()) {
            dbName = dbName + "_" + deleteMap.get("advCode");
            deleteMap.remove("advCode");
        }

        String basePath = GettqFilepath();
        String filePath = basePath + File.separator +  "DmgisPubData" + File.separator + dbName + File.separator + collectionName + File.separator;
        filePath = GetPath.action(deleteMap, filePath, collectionName);
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            file.delete();
            ++count;
            return (long)count;
        } else {
            return -1L;
        }
    }

    public long updateData(Map<String, Object> updateMap, Map<String, Object> updateData) {
        String dbName = dmgisConfig.getDbname();
        String collectionName = (String)updateMap.get("collectionName");
        updateMap.remove("collectionName");
//        if (StringUtil.IsNullOrEmpty((String)updateMap.get("advCode"))) {
        String advCode = (String)updateMap.get("advCode");
        if (Objects.nonNull(advCode) && !advCode.isEmpty()) {
            dbName = dbName + "_" + updateMap.get("advCode");
            updateMap.remove("advCode");
        }
        String basePath = GettqFilepath();
        String filePath = basePath + File.separator +  "DmgisPubData" + File.separator + dbName + File.separator + collectionName + File.separator;
        filePath = GetPath.action(updateMap, filePath, collectionName);
        byte[] fileByte = (byte[])((byte[])updateData.get("data"));
        File file = new File(filePath);
        if (!file.getParentFile().exists() && !file.getParentFile().isDirectory()) {
            file.getParentFile().mkdirs();
        }

        if (file.exists() && file.isFile()) {
            file.delete();
        }

        try {
            OutputStream os = new FileOutputStream(filePath);
            os.write(fileByte, 0, fileByte.length);
            os.flush();
            os.close();
            return 1L;
        } catch (IOException var11) {
            var11.printStackTrace();
            return 0L;
        }
    }

    public long existData(Map<String, Object> existMap) {
        String dbName = dmgisConfig.getDbname();
        String collectionName = (String)existMap.get("collectionName");
        existMap.remove("collectionName");
//        if (!StringUtil.IsNullOrEmpty((String)existMap.get("advCode"))) {
        String advCode = (String)existMap.get("advCode");
        if (Objects.nonNull(advCode) && !advCode.isEmpty()) {
            dbName = dbName + "_" + existMap.get("advCode");
            existMap.remove("advCode");
        }
        byte[] result = null;
        String basePath = GettqFilepath();
        String filePath = basePath + File.separator +  "DmgisPubData" + File.separator + dbName + File.separator + collectionName + File.separator;
        filePath = GetPath.action(existMap, filePath, collectionName);
        File file = new File(filePath);
        return file.exists() && file.isFile() ? 1 : 0;
    }

}
